import request from "@/utils/request";

const PermissionApi = new Map([
    ["/","permission/"],
    ["list", "permission/list"],
    ["save", "permission/save"],
    ["update", "permission/save"],
    ["delete", "permission/delete"],
    ["roles","permission/roles"]
]);

/**
 * 权限角色信息
 *
 * @param {number} perm_id 权限id
 */
const permissionRoles = (perm_id) => {
    return request.get(PermissionApi.get('roles'),{
        params:{
            perm_id
        }
    })
}

/**
 * 所有权限信息
 * 
 * @param {number} limit 每页展示的数据个数
 * @param {number} page 当前页码
 * @returns {Promise}
 */
const permissionList = (limit,page) => {
    return request.get(PermissionApi.get('/'),{
        params:{
            limit,
            page
        }
    });
}

/**
 * 所有权限信息(树形结构)
 * 
 * @returns {Promise}
 */
const permissionTreeList = () => {
    return request.get(PermissionApi.get('list'));
}

/**
 * 添加权限
 * 
 * @param {Object} data
 * @param {number} data.pid 父级权限id
 * @param {string} data.name 权限标识
 * @param {string} data.title 权限标题
 * @param {number} data.type 权限类型
 * @param {number} data.level 权限层级
 * @param {string} data.icon 权限图标
 * @param {string} data.path 路由路径
 * @param {number} data.status 权限状态(1启用,0为禁用)
 * @returns {Promise}
 */
const permissionAdd = (data) => {
    return request.post(PermissionApi.get('save'),data);
}

/**
 * 修改权限
 * 
 * @param {Object} data
 * @param {number} data.id 权限id
 * @param {number} data.pid 父级权限id
 * @param {string} data.name 权限标识
 * @param {string} data.title 权限标题
 * @param {number} data.type 权限类型
 * @param {number} data.level 权限层级
 * @param {string} data.icon 权限图标
 * @param {string} data.path 路由路径
 * @param {number} data.status 权限状态(1启用,0为禁用)
 * @returns {Promise}
 */
const permissionUpdate = (data) => {
    return request.post(PermissionApi.get('save'),data);
}

/**
 * 删除权限
 * 
 * @param {number} id 权限id
 * @returns {Promise}
 */
const permissionDelete = (id) => {
    return request.delete(PermissionApi.get('delete'),{
        params:{
            id
        }
    });
}

export {
    permissionAdd,
    permissionList,
    permissionTreeList,
    permissionUpdate,
    permissionDelete,
    permissionRoles
}